const validate = require('../../utils/validate')
const common = require('../../utils/common')
const { queryOrderBPage } = require('../../services/order')
Page({
  data: {
    page: {
      curPage: 0,
      pageSize: 10,
      pages: 0,
      total: 10,
    },
    query: {
      status: '',
    },
    orderList: [],
    loaded: false,
  },
  onLoad: function () {
    const status = 3
    this.setTitle(status)
    this.queryChange({ status })
  },
  setTitle: function (status) {
    if (status == 3) {
      wx.setNavigationBarTitle({ title: "待提货订单" })
    } else {
      wx.setNavigationBarTitle({ title: "商品订单" })
    }
  },
  updatePage: function (payload) {
    this.setData({
      page: { ...this.data.page, ...payload }
    })
  },
  queryChange: function (payload) {
    this.setData({
      query: { ...this.data.query, ...payload }
    }, this.getOrderList)
  },
  getOrderList: function (options) {
    if (this.data.orderList.length >= this.data.page.total) return
    const curPage = ++this.data.page.curPage
    const params = { ...this.data.page, curPage, ...this.data.query, ...options }
    queryOrderBPage(params).then(res => {
      this.updatePage(res.page)
      if (params.curPage === 1) {
        this.setData({
          orderList: res.data
        })
      } else {
        this.setData({
          orderList: [...this.data.orderList, ...res.data]
        })
      }
      this.setData({
        loaded: true
      })
    }).catch(err => {
      common.log(err)
    })
  },
  scrollBottom: function () {
    this.getOrderList()
  },
  goDetail: function (e) {
    const { index } = e.currentTarget.dataset;
    const item = this.data.orderList[index];
    wx.navigateTo({ url: `/pages/order-info/index?id=${item.orderId}` })
  },
  makePhoneCall: function (e) {
    const { phone } = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  goSell: function (e) {
    const { index } = e.currentTarget.dataset
    const item = this.data.orderList[index]
    wx.navigateTo({ url: `/pages/request-sell-service/index?id=${item.orderId}` })
  }
})